package com.westar.tv;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.westar.tv.utils.InstallationAPKUtil;
import com.westar.tv.utils.NetUtils;
import com.westar.tv.utils.RxPermissionsUtil;
import com.westar.tv.utils.SharePreferencesUtils;

import java.io.File;

/**
 * Created by wang-dev
 */
public class WelcomeActivity extends BaseActivity {

    private Handler handler = new Handler();

    private int count = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除title
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //去掉虚拟按键全屏显示
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        setContentView(R.layout.activity_welcome);

        RxPermissionsUtil.requestPermissions(this, Config.PERMISSION_LIST, new RxPermissionsUtil.IPermissionsCallBack() {
            @Override
            public void permissionErro(String name) {
                showShortToast("获取权限失败");
                CustomApplication.getInstance().appExit();
//                //延时跳转
//                handler.postDelayed(new ToMainActRunnable(), 1000);
            }

            @Override
            public void permissionSuccess(String name) {
                //延时跳转
                handler.postDelayed(new ToMainActRunnable(), 1000);
            }
        });
    }

    @Override
    protected void onResume() {
        boolean landscape = (Boolean) SharePreferencesUtils.get(this, SPKeys.DEFAULT_SP_FILE_NAME, SPKeys.SETTING_LANDSCAPE, false);
        if (landscape) {
            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        }
        super.onResume();
    }

    private class ToMainActRunnable implements Runnable {

        @Override
        public void run() {
            //检查网络是否连接
            if (NetUtils.isConnected(WelcomeActivity.this)) {
                //检查更新
                UpdateManager updateManager = new UpdateManager(WelcomeActivity.this);
                updateManager.checkUpdate();
            } else {

                if (count > 0) {
                    count--;

                    //5秒重试
                    final ToMainActRunnable t = new ToMainActRunnable();
                    handler.postDelayed(t, 10000);

                    //弹出提示
                    new MaterialDialog.Builder(WelcomeActivity.this)
                            .title(R.string.dialog_title_net_unconnected)
                            .content(Html.fromHtml(String.format(getString(R.string.dialog_content_net_unconnected), 10 - count)))
                            .cancelable(false)
                            .positiveText(R.string.dialog_btn_retry)
                            .negativeText(R.string.dialog_btn_exit)
                            .onPositive(new MaterialDialog.SingleButtonCallback() {
                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                    count = 10;
                                    dialog.cancel();
                                    //取消自动线程
                                    handler.removeCallbacks(t);
                                    handler.post(new ToMainActRunnable());
                                    dialog.dismiss();
                                }
                            })
                            .onNegative(new MaterialDialog.SingleButtonCallback() {
                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                    //退出程序
                                    app.appExit();
                                }
                            })
                            .build()
                            .show();
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == InstallationAPKUtil.REQUEST_CODE_APP_INSTALL) {
                File apkFile = new File(CustomApplication.installationAPKPath);
                if (null != apkFile) {
                    //安装apk
                    InstallationAPKUtil.installationAPK(this, apkFile);
                }
            }
        }
    }
}
